package com.main.listener.mq;

import com.main.common.CustomException;
import com.main.util.MailUtils;
import lombok.SneakyThrows;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RocketMQMessageListener(topic = "user_code", consumerGroup = "group_rocketmq")
public class MessageListener implements RocketMQListener<Map> {
    @Autowired
    private MailUtils mailUtils;

    @SneakyThrows
    @Override
    public void onMessage(Map map) {
        String mail = (String) map.get("mail");
        String code = (String) map.get("code");
        try {
            mailUtils.sendMail(mail, code);
            System.out.println("已完成短信发送业务,mail：" + mail);
//            System.out.println(Thread.currentThread().getId() + "--4");
        } catch (Exception e) {
            throw new CustomException("消息发送失败");
        }
    }
}
